//
//  NYGoodSubjectCorrectAnswerCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/5.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYGoodSubjectCorrectAnswerCell.h"
@interface NYGoodSubjectCorrectAnswerCell ()
@property(nonatomic, strong) QMUIGhostButton *optionBtn;
@property(nonatomic, strong) DTAttributedTextContentView *anwserLabel;
@property(nonatomic, strong) NYHtmlHelper *htmlHelper;
@property(nonatomic, strong) UIView *bottomContentView;
@property(nonatomic, strong) TTTAttributedLabel *titleLabel;
@end
@implementation NYGoodSubjectCorrectAnswerCell

- (BOOL)shouldUpdateCellWithObject:(NICellObject *)object atIndexPath:(NSIndexPath *)indexPath
{
        self.indexPath = indexPath;
        self.subjectModel = (NYSubjectModel *)object.userInfo;
        return YES;
}
- (void)setSubjectModel:(NYSubjectModel *)subjectModel
{
        _subjectModel = subjectModel;
        [self updateContent];
}
- (void)updateContent
{
        BOOL isFillBlanks = (self.subjectModel.question_type == NYQuestionTypeFillBlanks);
        self.anwserLabel.hidden = !isFillBlanks;
        self.optionBtn.hidden = isFillBlanks;
        
        if (isFillBlanks)
        {
                NSString *resString = [self.htmlHelper getImgSizeAndReplaceHtmlString:self.subjectModel.exercises_answer_arr[self.indexPath.row - 1]];
                self.anwserLabel.attributedString = [self.htmlHelper attributedStringForHtmlString:resString maxImageSize:CGSizeMake(__WGWidth(self.bottomContentView.width), CGFLOAT_MAX)];
                CGFloat suggestHeight = [self.anwserLabel  suggestedFrameSizeToFitEntireStringConstraintedToWidth:__MAIN_SCREEN_WIDTH__ - __WGWidth(32)].height;
                [self.anwserLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.top.left.right.bottom.equalTo(self.bottomContentView);
                        make.height.mas_equalTo(suggestHeight);
                }];
        }
        else
        {
                [self.optionBtn setTitle:[CoBaseUtils getLetterFromIndex:self.subjectModel.correct_answer_arr.firstObject] forState:(UIControlStateNormal)];
        }
}
- (void)setupView
{
        [super setupView];
        
        UIView *contentView = [[UIView alloc] init];
        [self.contentView addSubview:contentView];
        self.bottomContentView = contentView;
        [contentView addSubview:self.titleLabel];
        [contentView addSubview:self.optionBtn];
        [contentView addSubview:self.anwserLabel];
        
        [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).mas_offset(__WGHeight(16));
                make.left.equalTo(self.contentView).mas_offset(__WGHeight(16));
                make.bottom.equalTo(self.contentView).mas_offset(-__WGHeight(16)).priorityLow();
                make.right.equalTo(self.contentView).mas_offset(-__WGHeight(16));
        }];
        
        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.equalTo(contentView);
        }];
        
        [self.optionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(__WGHeight(16));
                make.left.equalTo(contentView);
                make.height.width.mas_equalTo(__WGHeight(21));
                make.bottom.lessThanOrEqualTo(contentView);
        }];
        
        [self.anwserLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(__WGHeight(16));
                make.left.right.bottom.equalTo(contentView);
        }];
}


/// MARK: - Lazy
- (TTTAttributedLabel *)titleLabel
{
        if (_titleLabel == nil) {
                _titleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
                _titleLabel.font = WGSystem14Font;
                _titleLabel.textColor = UIColorHex(0xFA8D00);
                _titleLabel.text = @"正确答案：";
        }
        return _titleLabel;
}
- (QMUIGhostButton *)optionBtn
{
        if (_optionBtn == nil) {
                _optionBtn = [[QMUIGhostButton alloc] init];
                _optionBtn.ghostColor = UIColorHex(0x5396F7);
                _optionBtn.titleLabel.font = WGSystem13Font;
        }
        return _optionBtn;
}
- (DTAttributedTextContentView *)anwserLabel
{
        if (_anwserLabel == nil) {
                _anwserLabel = [self.htmlHelper createAttributedContentView];
        }
        return _anwserLabel;
}

- (NYHtmlHelper *)htmlHelper
{
        if (_htmlHelper == nil) {
                _htmlHelper = [[NYHtmlHelper alloc] init];
        }
        return _htmlHelper;
}

@end
